package com.ecube.maintenance.components.fragment;


import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ecube.maintenance.R;
import com.ecube.maintenance.biz.apis.impl.CityAction;
import com.ecube.maintenance.biz.commons.Constants;
import com.ecube.maintenance.biz.commons.IListItem;
import com.ecube.maintenance.managers.PrefManager;
import com.ecube.maintenance.pojos.CityListInfo;
import com.google.gson.Gson;

import java.util.List;

//城市选择
public class CitySelectorFragment extends CustomFragment {

    public CitySelectorFragment() {
    }

    public static CitySelectorFragment newInstance() {
        CitySelectorFragment fragment = new CitySelectorFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
        }
        setFragmentTitle("城市选择");
    }

    private ActionBar getActionBar() {
        return getActivity().getActionBar();
    }

    @Override
    public void onResume() {
        super.onResume();
        configureActionBar();
    }

    private void configureActionBar() {
        ActionBar actionBar = getActionBar();
        assert actionBar != null;
        View v = actionBar.getCustomView();
        if (v != null) {
            v.setVisibility(View.GONE);
        }
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("城市选择");
        getActivity().invalidateOptionsMenu();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_city_selector, container, false);
        ((TextView) rootView.findViewById(R.id.cur_city))
                .setText(PrefManager.getInstance().getString(Constants.PREF_CITY_NAME));
        ListView stickyList = (ListView) rootView.
                findViewById(R.id.city_selector);
        stickyList.setAdapter(new CitySelectorAdapter(getActivity()));
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.clear();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    private class CitySelectorAdapter extends BaseAdapter {
        List<IListItem<CityListInfo.CityInfo>> cityData;
        private LayoutInflater inflater;

        public CitySelectorAdapter(Context context) {
            inflater = LayoutInflater.from(context);
            cityData = CityAction.getStaticCityList(context);
        }

        @Override
        public int getCount() {
            return cityData == null ? 0 : cityData.size();
        }

        @Override
        public Object getItem(int position) {
            return cityData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (cityData.get(position).isSection()) {
                return getHeaderView(convertView, parent, cityData.get(position).getSectionName());
            } else {
                return getCityView(convertView, parent, cityData.get(position).getInfo());
            }
        }

        public View getCityView(View convertView, ViewGroup parent, final CityListInfo.CityInfo cityInfo) {
            ViewHolder holder;
            if (convertView == null ||
                    !(convertView.getTag() instanceof ViewHolder)) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_list_city, parent, false);
                holder.text = (TextView) convertView;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(cityInfo.getName());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String json = new Gson().toJson(cityInfo, CityListInfo.CityInfo.class);
                    PrefManager pm = PrefManager.getInstance();
                    pm.putString(Constants.PREF_CITY_INFO, json);
                    pm.putString(Constants.PREF_CITY_NAME, cityInfo.getName());

                    getFragmentManager().popBackStack();
                }
            });
            return convertView;
        }

        public View getHeaderView(View convertView, ViewGroup parent, String section) {
            HeaderViewHolder holder;
            if (convertView == null ||
                    !(convertView.getTag() instanceof HeaderViewHolder)) {
                holder = new HeaderViewHolder();
                convertView = inflater.inflate(R.layout.item_list_city_header, parent, false);
                holder.text = (TextView) convertView;
                convertView.setTag(holder);
            } else {
                holder = (HeaderViewHolder) convertView.getTag();
            }
            holder.text.setText(section);
            return convertView;
        }

        class HeaderViewHolder {
            TextView text;
        }

        class ViewHolder {
            TextView text;
        }
    }
}
